app.controller('CollectionCtrl', ['$scope', '$q', '$interval', '$location', 'apiService',
    function(scope, $q, $interval, $location, apiService) {
		var newwindow = null;
		
		scope.collect = function(shopId){
			openTaobaoPage(shopId);
			apiService.get('/api/collection');
		};
		
		var openTaobaoPage = function(shopId) {
			var iWidth=500;                          //弹出窗口的宽度; 
	        var iHeight=200;               
			var iTop = (window.screen.availHeight - 30 - iHeight) / 2; 
			var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; 
			newwindow = window.open("https://favorite.taobao.com/popup/add_collection.htm?id="+shopId+"&itemtype=0","", 'height=' + iHeight + ',,innerHeight=' + iHeight + ',width=' + iWidth + ',innerWidth=' + iWidth + ',top=' + iTop + ',left=' + iLeft + ',status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=0,titlebar=no');			
		};
		
		var checkNewWindowStatus = function() {
			if (newwindow && newwindow.closed == true) {
				$interval.cancel(myInterval); 
				window.location.href =$("#redirecturl").val();
			}
		};
		
		var myInterval = $interval(checkNewWindowStatus, 1000);
	}
]);